#include "../include/netdisk.h"

int testLog(int num, const char *funName, const char *fileName)
{
    int fd = open("/home/hsh/project_cpp/Server/src/test.log", O_WRONLY|O_CREAT|O_APPEND, 0666);
    if(fd < 0)
    {
        return -1;// open test.log failed: No such file or directory
    }
    
    char cwd[1024];
    getcwd(cwd, sizeof(cwd));
    printf("当前目录(test.log生成位置): %s\n", cwd); // 确认test.log生成位置

    time_t now = time(NULL);
    struct tm* curtime = localtime(&now);
    int year = curtime->tm_year + 1900;
    int month = curtime->tm_mon + 1;
    int day = curtime->tm_mday;
    int hour = curtime->tm_hour;
    int min = curtime->tm_min;
    int second = curtime->tm_sec;
    char buf[4096] = {0};
    
    sprintf(buf + strlen(buf), "=== Start ===\n");
    sprintf(buf + strlen(buf), "%d年-%02d月-%02d日  %02d:%02d:%02d\n", year, month, day, hour, min, second);
    sprintf(buf + strlen(buf), "File: %s\n", fileName);
    sprintf(buf + strlen(buf), "Line: %d\n", num);
    sprintf(buf + strlen(buf), "Function: %s\n", funName);
    // sprintf(buf + strlen(buf), "=== End ===\n");

    printf("准备写日志!\n");
    write(fd, buf, strlen(buf));
    close(fd);
    return 0;
}